package com.bosssoft.cloud.wechat.core.infra.enums;

import lombok.Getter;

/**
 * wechat Api Enum
 * 后续扩展中，微信所有api放到此Enum下。
 * <p>
 * api详情参考微信官方文档：https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
 *
 * @author wcy create on 2021/6/18 15:07 下午
 */
public interface WxApiUrl {

    /**
     * 获得完整url
     * ps:jdk1.7无法使用default
     */
    String getUrl();

    String getType();

    /**
     * 网页授权
     * 第一步和第三步尚未测试，极小概率会有请求类型错误等问题。
     * 参考：https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#4
     */
    @Getter
    enum OAuth2 implements WxApiUrl {
        /* 第一步：用户同意授权，获取code */
        CONNECT_OAUTH2_AUTHORIZE_URL("GET", "https://open.weixin.qq.com", "/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s&connect_redirect=1#wechat_redirect"),
        /* 第二步：通过code换取网页授权access_token */
        OAUTH2_ACCESS_TOKEN_URL("GET", "https://api.weixin.qq.com", "/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code"),
        /* 第三步：刷新access_token（如果需要） */
        OAUTH2_REFRESH_TOKEN_URL("GET", "https://api.weixin.qq.com", "/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s"),
        /* 第四步：拉取用户信息(需scope为 snsapi_userinfo) */
        OAUTH2_USERINFO_URL("GET", "https://api.weixin.qq.com", "/sns/userinfo?access_token=%s&openid=%s&lang=%s"),
        /* 附：检验授权凭证（access_token）是否有效 */
        OAUTH2_VALIDATE_TOKEN_URL("GET", "https://api.weixin.qq.com", "/sns/auth?access_token=%s&openid=%s");

        private final String type;
        private final String prefix;
        private final String path;

        OAuth2(String type, String prefix, String path) {
            this.type = type;
            this.prefix = prefix;
            this.path = path;
        }

        @Override
        public String getUrl() {
            return this.prefix + this.path;
        }
    }

    /**
     * 签名授权
     * 注：access_token是公众号的全局唯一接口调用凭据，公众号调用各接口时都需使用access_token
     * <p>
     * ps:
     * JS-SDK使用权限签名算法流程：
     * 1.获取token;
     * 2.用token获取ticket;
     * 3.用ticket,noncestr（随机字符串）,timestamp（时间戳）,url（当前网页的URL，不包含#及其后面部分）做sha1加密获取signature
     * 参考：https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62
     */
    @Getter
    enum Signature implements WxApiUrl {
        /* 获取Access token */
        GET_ACCESS_TOKEN_URL("GET", "https://api.weixin.qq.com", "/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"),
        /* 获得jsapi_ticket */
        GET_TICKET_URL("GET", "https://api.weixin.qq.com", "/cgi-bin/ticket/getticket?type=jsapi&access_token=%s");

        private final String type;
        private final String prefix;
        private final String path;

        Signature(String type, String prefix, String path) {
            this.type = type;
            this.prefix = prefix;
            this.path = path;
        }

        @Override
        public String getUrl() {
            return this.prefix + this.path;
        }
    }

    /**
     * 模板消息
     * 参考https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
     */
    @Getter
    enum Template implements WxApiUrl {
        /* 设置所属行业 */
        TEMPLATE_API_SET_INDUSTRY("POST", "https://api.weixin.qq.com", "/cgi-bin/template/api_set_industry?access_token=%s"),
        /* 获取设置的行业信息 */
        TEMPLATE_GET_INDUSTRY("GET", "https://api.weixin.qq.com", "/cgi-bin/template/get_industry?access_token=%s"),
        /* 获得模板ID */
        TEMPLATE_API_ADD_TEMPLATE("POST", "https://api.weixin.qq.com", "/cgi-bin/template/api_add_template?access_token=%s"),
        /* 获取模板列表 */
        TEMPLATE_GET_ALL_PRIVATE_TEMPLATE("GET", "https://api.weixin.qq.com", "/cgi-bin/template/get_all_private_template?access_token=%s"),
        /* 删除模板 */
        TEMPLATE_DEL_PRIVATE_TEMPLATE("POST", "https://api.weixin.qq.com", "/cgi-bin/template/del_private_template?access_token=%s"),
        /* 发送模板消息 */
        MESSAGE_TEMPLATE_SEND("POST", "https://api.weixin.qq.com", "/cgi-bin/message/template/send?access_token=%s");

        private final String type;
        private final String prefix;
        private final String path;

        Template(String type, String prefix, String path) {
            this.type = type;
            this.prefix = prefix;
            this.path = path;
        }

        @Override
        public String getUrl() {
            return this.prefix + this.path;
        }
    }

    /**
     * 菜单相关
     * 参考：https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
     */
    @Getter
    enum Menu implements WxApiUrl {
        /* 创建菜单，每次更新菜单时调用此接口 */
        MENU_CREATE("POST", "https://api.weixin.qq.com", "/cgi-bin/menu/create?access_token=%s"),
        /* 查询接口 */
        GET_CURRENT_SELFMENU_INFO("GET", "https://api.weixin.qq.com", "/cgi-bin/get_current_selfmenu_info?access_token=%s"),
        /* 删除接口 */
        MENU_DELETE("GET", "https://api.weixin.qq.com", "/cgi-bin/menu/delete?access_token=%s"),
        /* 创建个性化菜单 */
        MENU_ADDCONDITIONAL("POST", "https://api.weixin.qq.com", "/cgi-bin/menu/addconditional?access_token=%s"),
        /* 删除个性化菜单 */
        MENU_DELCONDITIONAL("POST", "https://api.weixin.qq.com", "/cgi-bin/menu/delconditional?access_token=%s"),
        /* 测试个性化菜单匹配结果 */
        MENU_TRYMATCH("POST", "https://api.weixin.qq.com", "/cgi-bin/menu/trymatch?access_token=%s"),
        /* 获取自定义菜单配置 */
        MENU_GET("GET", "https://api.weixin.qq.com", "/cgi-bin/menu/get?access_token=%s");

        private final String type;
        private final String prefix;
        private final String path;

        Menu(String type, String prefix, String path) {
            this.type = type;
            this.prefix = prefix;
            this.path = path;
        }

        @Override
        public String getUrl() {
            return this.prefix + this.path;
        }
    }
}
